package it.fantacalcio.struts.action.amministrazione.user;

import java.util.ArrayList;
import java.util.List;

import it.fantacalcio.model.Role;
import it.fantacalcio.model.User;
import it.fantacalcio.spring.service.MyServiceFactory;
import it.fantacalcio.struts.form.UserForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class PrepareCRUDUserAction extends DispatchAction {
	
	@Override
	protected ActionForward dispatchMethod(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest arg2, HttpServletResponse arg3, String arg4)
			throws Exception {
		
		User userInSession = (User) arg2.getSession().getAttribute("userInSession");
		if(userInSession==null || userInSession.getAttivo().equals("NO")){
			return arg0.findForward("noninsession");
		}
		return super.dispatchMethod(arg0, arg1, arg2, arg3, arg4);
	}
	
	public ActionForward aggiungi(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		User userInSession = (User) request.getSession().getAttribute("userInSession");
		if(!userInSession.getRole().getCodRole().equals("ADMIN")){
			return mapping.findForward("noninsession");
		}
		
		List<Role> listaRole = new ArrayList<Role>();
		try {
			listaRole = MyServiceFactory.getRoleService().list();
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
		request.setAttribute("listaRole", listaRole);
		return mapping.findForward("aggiungi");
	}
	
	public ActionForward modifica(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		User userInSession = (User) request.getSession().getAttribute("userInSession");
		if(!userInSession.getRole().getCodRole().equals("ADMIN")){
			return mapping.findForward("noninsession");
		}
		
		UserForm userForm=(UserForm)form;
		Long idUser = Long.parseLong(request.getParameter("id"));
		User user = new User();
		List<Role> listaRole = new ArrayList<Role>();
		try {
			user = MyServiceFactory.getUserService().get(idUser);
			BeanUtils.copyProperties(userForm, user);
			userForm.setIdRole(user.getRole().getId());
			listaRole = MyServiceFactory.getRoleService().list();
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
		request.setAttribute("listaRole", listaRole);
		return mapping.findForward("modifica");
	}
	
	public ActionForward cancella(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		User userInSession = (User) request.getSession().getAttribute("userInSession");
		if(!userInSession.getRole().getCodRole().equals("ADMIN")){
			return mapping.findForward("noninsession");
		}
		
		Long idUser = Long.parseLong(request.getParameter("id"));
		User user = new User();
		try {
			user = MyServiceFactory.getUserService().get(idUser);
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
		request.setAttribute("user", user);
		return mapping.findForward("cancella");
	}
	
	public ActionForward apri(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Long idUser = Long.parseLong(request.getParameter("id"));
		User user = new User();
		try {
			user = MyServiceFactory.getUserService().get(idUser);
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
		request.setAttribute("user", user);
		return mapping.findForward("apri");
	}

}
